import { createRouter, createWebHistory } from "vue-router";
import {useInfoStore} from '../store'

export const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: "/",//$route.params.username 页面接受这个参数
      redirect: "index",
      name:'user',
      component: () => import("../LayOut/index.vue"),
      children: [
        {
          path: 'index',
          name:'index',
          component:()=> import("../views/index/index.vue"),
          meta:{
            keepAlive:true
          }
      },
        // {
        //     path: '/',
        //     name:'test',
        //     component:()=> import("../views/index.vue"),
        //     meta:{
        //       keepAlive:true
        //     }
        // },
        {
            path: 'priceChar',
            component:()=> import("../views/priceChar/index.vue"),
            name:'test'
        },
        {
          path: 'three',
          component:()=> import("../views/Three.vue"),
          name:'three'
      },
      {
        path: 'chatgpt',
        component:()=> import("../views/chatgpt/chatgpt.vue"),
        name:'chatgpt'
    },
      ]
    },
    {
       path: '/login',
       component:()=> import("../views/Login.vue"),
          name:'login'
    }
   
  ],
});

//路由守卫，必须store里有token才允许跳转
router.beforeEach((to, from) => {

  const store = useInfoStore()
    
    if(to.name!=='login' && store.token==="" ) 
    return {name:'login'}
})